
package com.jf.cloud.product.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.product.bo.PlatformCommissionItemBO;
import com.jf.cloud.api.product.bo.PlatformCommissionOrderItemBO;
import com.jf.cloud.api.product.dto.CategoryShopDTO;
import com.jf.cloud.api.product.feign.CategoryShopFeignClient;
import com.jf.cloud.common.product.vo.CategoryVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.product.bo.CategoryRateBO;
import com.jf.cloud.product.mapper.CategoryMapper;
import com.jf.cloud.product.service.CategoryService;
import com.jf.cloud.product.service.CategoryShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zz
 * @date 2021/5/24
 */
@RestController
@Hidden
public class CategoryShopFeignController implements CategoryShopFeignClient {

    @Autowired
    private CategoryShopService categoryShopService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponseEntity<List<PlatformCommissionOrderItemBO>> calculatePlatformCommission(List<PlatformCommissionOrderItemBO> platformCommissionOrderItems) {

        // 如果为空表示为从店铺改价过来，需要先获取分类ids
        if(Objects.isNull(platformCommissionOrderItems.get(0).getCategoryId())){
            List<PlatformCommissionOrderItemBO> rateOrderItemList = categoryService.listBySkuIds(platformCommissionOrderItems);
            Map<Long, Long> rateMap = rateOrderItemList.stream().collect(Collectors.toMap(PlatformCommissionOrderItemBO::getSkuId, PlatformCommissionOrderItemBO::getCategoryId));
            for (PlatformCommissionOrderItemBO platformCommissionOrderItem : platformCommissionOrderItems) {
                platformCommissionOrderItem.setCategoryId(rateMap.get(platformCommissionOrderItem.getSkuId()));
            }
        }

        for (PlatformCommissionOrderItemBO platformCommissionOrderItem : platformCommissionOrderItems) {

            // 处理商家端的分类关联,获取店铺各个分类的费率
            PlatformCommissionItemBO shopItem = platformCommissionOrderItem.getShopItem();
            List<CategoryRateBO> shopCategoryRates = categoryShopService.listRateByShopId(shopItem.getShopId(),SysTypeEnum.MULTISHOP.value());
            if (CollectionUtil.isEmpty(shopCategoryRates)) {
                continue;
            }
            for (CategoryRateBO shopCategoryRate : shopCategoryRates) {
                if (Objects.equals(platformCommissionOrderItem.getCategoryId(), shopCategoryRate.getCategoryId()) && shopCategoryRate.getRate() != null) {
                    platformCommissionOrderItem.setRate(shopCategoryRate.getRate());
                }
            }

            if(Objects.isNull(platformCommissionOrderItem.getSupplierItem())){
                continue;
            }

            // 处理供应商端的分类关联
            // 获取供应商各个分类的费率
            PlatformCommissionItemBO supplierItem = platformCommissionOrderItem.getSupplierItem();
            List<CategoryRateBO> supplierCategoryRates = categoryShopService.listRateByShopId(supplierItem.getShopId(),SysTypeEnum.SUPPLIER.value());
            if (CollectionUtil.isEmpty(supplierCategoryRates)) {
                continue;
            }
            for (CategoryRateBO supplierCategoryRate : supplierCategoryRates) {
                // 分类id相同，用平台的佣金
                if (Objects.equals(platformCommissionOrderItem.getCategoryId(), supplierCategoryRate.getCategoryId()) && supplierCategoryRate.getRate() != null) {
                    platformCommissionOrderItem.setSupplierRate(supplierCategoryRate.getRate());
                }
            }
        }
        return ServerResponseEntity.success(platformCommissionOrderItems);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSupplierIdsByShopId(Long shopId) {
        return ServerResponseEntity.success(categoryShopService.getSupplierIdsByShopId(shopId));
    }

    @Override
    public ServerResponseEntity<List<Long>> getCategoryIdsByShopId(Long shopId, Integer sysType) {
        return ServerResponseEntity.success(categoryShopService.getCategoryIdsByShopId(shopId,sysType));
    }

    @Override
    public ServerResponseEntity<Void> insertBatchByShopId(List<CategoryShopDTO> categoryShopDTOList, Long shopId, Integer sysType) {
        categoryShopService.insertBatchByShopId(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<Long>> getOneCategoryIdByShopId(Long shopId, Integer sysType,Integer lang) {
        List<CategoryVO> threeCategoryList = categoryMapper.listSigningCategoryByShopIdAndStatus(shopId, sysType,null);
        if (Objects.isNull(threeCategoryList) || threeCategoryList.size() == 0) {
            // 签约分类为空时
            return  ServerResponseEntity.success(null);
        }
//        ProductLangUtil.categoryList(threeCategoryList);
//        List<CategoryVO> twoCategoryList = categoryMapper.getListByCategoryIds(threeCategoryList.stream().map(CategoryVO::getParentId).collect(Collectors.toSet()));
//        ProductLangUtil.categoryList(twoCategoryList);
//        List<CategoryVO> oneCategoryList = categoryMapper.getListByCategoryIds(twoCategoryList.stream().map(CategoryVO::getParentId).collect(Collectors.toSet()));
//        ProductLangUtil.categoryList(oneCategoryList);
//        //一级分类id集合
//        List<Long> oneCategoryId = new ArrayList<>();
//        for (CategoryVO categoryVO : oneCategoryList) {
//            oneCategoryId.add(categoryVO.getCategoryId());
//        }
        return ServerResponseEntity.success(threeCategoryList.stream().map(CategoryVO::getCategoryId).collect(Collectors.toList()));
//        return ServerResponseEntity.success(oneCategoryId);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSupplyIdByCategoryIds(List<Long> categoryIds) {
        return ServerResponseEntity.success(categoryShopService.getSupplyIdByCategoryIds(categoryIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> getShopIdByCategoryIds(Object[] categoryIds,Object[] shopIds) {
        return ServerResponseEntity.success(categoryShopService.getShopIdByCategoryIds(categoryIds,shopIds));
    }
}
